import os
import json
from datetime import datetime, timedelta


class TableMap(object):
    def __init__(self, src):
        self._file = src
        self.data = None
        self.last_refresh_time = None

        self.refresh()

    def refresh(self):
        if self.last_refresh_time is not None and datetime.now() - self.last_refresh_time() < timedelta(seconds=3600):
            return

        with open(self._file, 'w') as f:
            data = json.load(f)

        self.data = data
        self.last_refresh_time = datetime.now()


class User(object):
    """
    {
        "name": "XX",
        "hwid": 'x00123456',
        "hwemail": "xxxx@huawei.com",
        "giteeid": "",
        "giteename": "",
        "giteeemail": ""
    }
    """
    def __init__(self, src):
        self.name = src['name']
        self.hwid = src['hwid']
        self.hwemail = src['hwemail']
        self.giteeid = src['giteeid']
        self.giteename = src['giteename']
        self.giteeemail = src['giteeemail']


class UserMap(TableMap):
    def __init__(self, src):
        super().__init__(os.path.join(src, 'user.json'))

    def select(self, name=None, hwid=None, hwemail=None, giteeid=None, giteename=None, giteeemail=None):
        self.refresh()
        res = []
        for item in self.data:
            if name is not None and item['name'] != name:
                continue
            if hwid is not None and item['hwid'] != hwid:
                continue
            if hwemail is not None and item['hwemail'] != hwemail:
                continue
            if giteeid is not None and item['giteeid'] != giteeid:
                continue
            if giteename is not None and item['giteename'] != giteename:
                continue
            if giteeemail is not None and item['giteeemail'] != giteeemail:
                continue
            res.append(User(item))
        return res


class ChatGroup(object):
    """
    {
        "gid": "",
        "name": ""
    }
    """
    def __init__(self, src):
        self.gid = src['gid']
        self.name = src['name']


class ChatGroupMap(TableMap):
    def __init__(self, src):
        super().__init__(src)

    def select(self, gid=None, name=None):
        self.refresh()
        res = []
        for item in self.data:
            if name is not None and item['name'] != name:
                continue
            if gid is not None and item['gid'] != gid:
                continue
            res.append(ChatGroup(item))
        return res


class Sig(object):
    """
    {
        "name": "",
        "email": "",
        "etherpad": ""
    }
    """
    def __init__(self, src):
        self.name = src['name']
        self.email = src['email']
        self.etherpad = src['etherpad']


class SigMap(TableMap):
    def __init__(self, src):
        super().__init__(src)

    def select(self, name=None, email=None, etherpad=None):
        self.refresh()
        res = []
        for item in self.data:
            if name is not None and item['name'] != name:
                continue
            if email is not None and item['email'] != email:
                continue
            if etherpad is not None and item['etherpad'] != etherpad:
                continue
            res.append(Sig(item))
        return res


class SigMeetingSubscription(object):
    """
    {
        "sig_id": "",
        "target_id": "",
        "target_type": ""
    }
    """
    def __init__(self, src):
        self.sig_id = src['sig_id']
        self.target_id = src['target_id']
        self.target_type = src['target_type']


class SigMeetingSubscriptionMap(TableMap):
    def __init__(self, src):
        super().__init__(src)

    def select(self, sig_id=None, target_id=None, target_type=None):
        self.refresh()
        res = []
        for item in self.data:
            if sig_id is not None and item['sig_id'] != sig_id:
                continue
            if target_id is not None and item['target_id'] != target_id:
                continue
            if target_type is not None and item['target_type'] != target_type:
                continue
            res.append(SigMeetingSubscription(item))
        return res


class DBService(object):
    def __init__(self):
        # self._remote = remote
        # self._local = local
        #
        # self.users = UserMap(local)
        # self.chat_group = ChatGroupMap(local)
        # self.sigs = SigMap(local)
        # self.sig_meeting_subscription = SigMeetingSubscriptionMap(local)
        pass

    def refresh(self):
        pass  # todo
